package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.yeyaomai.dksns.flowis.domain.Module;
import com.yeyaomai.dksns.flowis.service.DownExcelService;
import com.yeyaomai.dksns.flowis.service.ModuleService;
import com.yeyaomai.dksns.flowis.service.result.DynamicXml;
import com.yeyaomai.dksns.flowis.util.Globals;
import com.yeyaomai.dksns.flowis.util.StringUtil;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

@Controller
@RequestMapping("/module/*")
public class ModuleController {

	@Autowired
	private ModuleService moduleService;
	
	@Autowired
	private DownExcelService downExcelService;
	
	@RequestMapping(value = "/detail")
	public ModelAndView getModuleDetail(HttpServletRequest request)
			throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		String formDate = request.getParameter("formDate");
		String toDate = request.getParameter("toDate");
		String number = request.getParameter("number");
        String type = request.getParameter("type");
		String date = formDate;
		map.put("dateCount", 2);
		ModelAndView view = new ModelAndView("module-detail");
		view.addObject("formDate", formDate);
		view.addObject("toDate", toDate);
		view.addObject("type",type);
		view.addObject("nowDate", Globals.FORMAT_TYPE_A.format(new Date()));
		view.addObject("month", VelocityToolDateUtils.getDay(Integer.valueOf("-"+Globals.FORMAT_TYPE_D.format(new Date()))+1));
		if (formDate.equals(toDate)) {
			map.put("dateCount", 1);
		}
		toDate = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(
				toDate).getTime()
				+ 1 * 24 * 3600 * 1000);

		map.put("fromReferDay", date);
		map.put("toReferDay", toDate);
		Page<Module> result = moduleService.getDataPage(map,
				Integer.valueOf(number));
		view.addObject("result", result);
		view.addObject("number", number);
		return view;
	}
	
	
	@RequestMapping(value = "/modulepiexml")
	public void changePieXml(@RequestParam("fromDate") String fromDate,
			@RequestParam("toDate") String toDate, HttpServletResponse response) throws ParseException, IOException
		{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Module> module = new ArrayList<Module>();
		map.put("dateCount", 2);
		String date = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(
				toDate).getTime()
				+ 1 * 24 * 3600 * 1000);
		map.put("fromReferDay", fromDate);
		map.put("toReferDay", date);
		map.put("startNumber", null);
		map.put("endNumber", null);
		module = moduleService.getModuleByDay(map);
		String xml = DynamicXml.getModulePie(module,fromDate,date);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
	   }
	
	
	@RequestMapping(value = "/modulehistoryxml")
	public void changeModuleHistoryXml(@RequestParam("fromDate") String fromDate,
			@RequestParam("value") String value,
			@RequestParam("toDate") String toDate, @RequestParam("moduleName") String moduleName,HttpServletResponse response) throws IOException, ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("moduleName", moduleName);
		map.put("fromDate", fromDate);
		map.put("toDate", toDate);

		List<Module> moduleList =moduleService.getModuleByModuleName(map);
		String xml = DynamicXml.getColumnByModule(moduleList, value, fromDate,toDate);

		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
	}
	
	@RequestMapping(value = "/history")
	public ModelAndView getModuleHistory(HttpServletRequest request)
			throws ParseException, UnsupportedEncodingException {
		String moduleName = request.getParameter("moduleName");
		String toDate = request.getParameter("date");
		Date date = Globals.FORMAT_TYPE_A.parse(toDate);
		Date d = null;
		String type = request.getParameter("type");
		String fromDate = "";
		if(type.equals("-1"))
		{
			d = DateUtils
					.truncate(DateUtils.addDays(date, -30), Calendar.DATE);
			fromDate = Globals.FORMAT_TYPE_A.format(d);
		}
		else if(type.equals("1"))
		{
			d = DateUtils
					.truncate(DateUtils.addDays(date, 30), Calendar.DATE);
			String middle = toDate;
			toDate = Globals.FORMAT_TYPE_A.format(d);
			fromDate = middle;
		}
		String currentTime = Globals.FORMAT_TYPE_A.format(new Date());
		ModelAndView view = new ModelAndView("module-history");

		if((Globals.FORMAT_TYPE_A.parse(toDate).getTime()) >= (Globals.FORMAT_TYPE_A.parse(currentTime).getTime()))
		{
			view.addObject("show", 1);
		}
		else
		{
			view.addObject("show", 0);
		}
		view.addObject("moduleName",moduleName);
		view.addObject("currentDate",currentTime );
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("moduleName", moduleName);
		map.put("fromDate", fromDate);
		map.put("toDate", toDate);
		List<Module> moduleList = new ArrayList<Module>();
		moduleList = moduleService.getModuleByModuleName(map);
		view.addObject("moduleList", moduleList);
		view.addObject("fromDate", fromDate);
		view.addObject("toDate", toDate);
		return view;
	}
	
	 // 网络接入商报表下载
	@RequestMapping(value = "down")
	public void download(HttpServletRequest request,
			HttpServletResponse response) throws ParseException,
			IOException {

		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String todate = Globals.FORMAT_TYPE_A.format(Globals.FORMAT_TYPE_A.parse(endTime).getTime() + 1 * 24 * 3600 * 1000);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fromReferDay", startTime);
		map.put("toReferDay", todate);
		map.put("startNumber", null);
		map.put("endNumber", null);
		map.put("dateCount", 1);
		List<Module> moduleList = moduleService.getModuleByDay(map);
        for (Module module:moduleList) {
			
			module.setModuleUserName(StringUtil.getModule(module.getModuleName().toString()));
			
		}
		String title = "模块管理(" + startTime + "至" + endTime + ")";
		String newTitle = URLEncoder.encode(title, "UTF-8");

		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="
				+ newTitle + ".xls");

		List<String> heads = new ArrayList<String>();
		heads.add("模块名称");
		heads.add("访问次数");
		heads.add("独立访客");
		heads.add("ip");
		heads.add("新独立访客");
		
		List<String> pros = new ArrayList<String>();
		pros.add("moduleUserName");
		pros.add("visitCount");
		pros.add("uniqueHost");
		pros.add("ip");
		pros.add("newUniqueHost");
		try {
			downExcelService.exportExcel(title, heads, moduleList,
					response.getOutputStream(), pros);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
